#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import wsgiref.handlers

import datetime

from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp


class MainHandler(webapp.RequestHandler):

	def get(self):
		user = users.get_current_user()

		if not user:
			self.redirect(users.create_login_url(self.request.uri))
		self.render(self.getTemplateFilename(), self.getTemplateValues())

	def getTemplateValues(self):
		user = users.get_current_user()
		nickname = user.nickname()
		template_values = {
			'name': 'ucsg main page',
			'nickname': nickname
		}
		return template_values

	def getTemplateFilename(self):
			return "index.html"

	def render(self, filename, template_values):
		path = os.path.join(os.path.dirname(__file__), 'templates', filename)
		self.response.out.write(template.render(path, template_values))


class CalendarPage(MainHandler):

	def get(self):
		self.render(self.getTemplateFilename(), self.getTemplateValues())

	def get_applicable_dates(self):
		current_date = datetime.date.today()
		month = self.request.get('month')
		year = self.request.get('year')
		year_type = type(year)
		if month and year:
			self.response.out.write(year_type)
			self.response.out.write(year)
			current_date = datetime.date(2007,10,1)
		return current_date
		
	def getTemplateValues(self):
		user = users.get_current_user()
		nickname = user.nickname()
		the_date = self.get_applicable_dates()
		template_values = {
			'name': 'My Calendar',
			'nickname': nickname,
			'date': the_date
		}
		return template_values
		
	def getTemplateFilename(self):
		return "calendar.html"
		
	def render(self, filename, template_values):
		path = os.path.join(os.path.dirname(__file__), 'templates', filename)
		self.response.out.write(template.render(path, template_values))



def main():
	application = webapp.WSGIApplication([
		('/', MainHandler),
		('/calendar',CalendarPage)],
		debug=True)
	wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
